package com.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.entity.Menu;
import com.health.mapper.MenuMapper;
import com.health.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Map<String, Object> selectMenuList(HashMap<String, Object> param) {
        // 查询出顶部菜单
        List<Menu> topMenus = menuMapper.selectMenuList(param);
        String role = String.valueOf(param.get("role"));
        topMenus.forEach(item -> {
            // 查询出一级菜单
            Integer topMenuIp = item.getId();
            Map queryParam = new HashMap();
            queryParam.put("parent_id", topMenuIp);
            queryParam.put("role_id", role);
            List<Menu> menu = menuMapper.selectList(new QueryWrapper<>().allEq(queryParam));
            menu.forEach(children -> {
                // 查询一级菜单下的子菜单
                List<Menu> childrenMenu = menuMapper.selectList(new QueryWrapper<Menu>().eq("parent_id", children.getId()).eq("role_id", role));
                children.setChild(childrenMenu);
            });
            item.setChild(menu);
        });
        Map<String, Object> result = new HashMap<>();
        result.put("homeInfo", getHomeInfo(role));
        result.put("logoInfo", getLogoInfo(role));
        result.put("menuInfo", topMenus);
        return result;
    }

    @Override
        public R selectMainMenu(HashMap<String, Object> param) {
            String role = (String)param.get("role");
            return R.ok(menuMapper.selectOne(new QueryWrapper<Menu>().eq("is_menu", 1).eq("role_id", role)));
        }

        private Map<String, Object> getHomeInfo(String role) {
            return menuMapper.selectHomePage(role);
        }

        private Map<String, Object> getLogoInfo(String role) {
            return menuMapper.selectLogoConfig(role);
        }
}
